package com.lxmes.sales.service.impl;

import java.util.List;
import com.lxmes.common.utils.DateUtils;
import com.lxmes.common.utils.NewNumber;
import com.lxmes.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.NcContractCreationApprovalMapper;
import com.lxmes.sales.domain.NcContractCreationApproval;
import com.lxmes.sales.service.INcContractCreationApprovalService;

/**
 * 合同创建与审批Service业务层处理
 *
 * @author lx
 * @date 2023-06-06
 */
@Service
public class NcContractCreationApprovalServiceImpl implements INcContractCreationApprovalService
{
    @Autowired
    private NcContractCreationApprovalMapper ncContractCreationApprovalMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询合同创建与审批
     *
     * @param id 合同创建与审批主键
     * @return 合同创建与审批
     */
    @Override
    public NcContractCreationApproval selectNcContractCreationApprovalById(Long id)
    {
        return ncContractCreationApprovalMapper.selectNcContractCreationApprovalById(id);
    }

    /**
     * 查询合同创建与审批列表
     *
     * @param ncContractCreationApproval 合同创建与审批
     * @return 合同创建与审批
     */
    @Override
    public List<NcContractCreationApproval> selectNcContractCreationApprovalList(NcContractCreationApproval ncContractCreationApproval)
    {
        return ncContractCreationApprovalMapper.selectNcContractCreationApprovalList(ncContractCreationApproval);
    }

    /**
     * 新增合同创建与审批
     *
     * @param ncContractCreationApproval 合同创建与审批
     * @return 结果
     */
    @Override
    public int insertNcContractCreationApproval(NcContractCreationApproval ncContractCreationApproval)
    {

        ncContractCreationApproval.setCreateTime(DateUtils.getNowDate());
        ncContractCreationApproval.setContractNumber("CG"+ NewNumber.createNewNumber(ncContractCreationApprovalMapper.getNewCode(DateUtils.getDate())));
        ncContractCreationApproval.setCreateBy(SecurityUtils.getUsername());
        return ncContractCreationApprovalMapper.insertNcContractCreationApproval(ncContractCreationApproval);
    }

    /**
     * 修改合同创建与审批
     *
     * @param ncContractCreationApproval 合同创建与审批
     * @return 结果
     */
    @Override
    public int updateNcContractCreationApproval(NcContractCreationApproval ncContractCreationApproval)
    {
        ncContractCreationApproval.setUpdateTime(DateUtils.getNowDate());
        return ncContractCreationApprovalMapper.updateNcContractCreationApproval(ncContractCreationApproval);
    }

    /**
     * 批量删除合同创建与审批
     *
     * @param ids 需要删除的合同创建与审批主键
     * @return 结果
     */
    @Override
    public int deleteNcContractCreationApprovalByIds(Long[] ids)
    {
        return ncContractCreationApprovalMapper.deleteNcContractCreationApprovalByIds(ids);
    }

    /**
     * 删除合同创建与审批信息
     *
     * @param id 合同创建与审批主键
     * @return 结果
     */
    @Override
    public int deleteNcContractCreationApprovalById(Long id)
    {
        return ncContractCreationApprovalMapper.deleteNcContractCreationApprovalById(id);
    }
    @Override
    public String imporIteminfo(List<NcContractCreationApproval> ncContractCreationApprovalList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(ncContractCreationApprovalList) || ncContractCreationApprovalList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (NcContractCreationApproval ncContractCreationApproval : ncContractCreationApprovalList)
        {
            try
            {
                BeanValidators.validateWithException(validator, ncContractCreationApproval);
                ncContractCreationApproval.setCreateBy(operName);
                ncContractCreationApproval.setUpdateBy(operName);
                this.insertNcContractCreationApproval(ncContractCreationApproval);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
